/**
 * Created by chenqiming on 16/4/16.
 */

import  {
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
} from 'react-native';
import React from 'react';

import pinyin from 'pinyin';
import styles from './style';
import RightItemHeight from './RightItemHeight';
import Toast from '../../utils/Toast';
export default class MainList extends React.Component {
    static propTypes = {
        dataSource: React.PropTypes.object
    };
    constructor(props){
        super(props);
        RightItemHeight.splice(0,RightItemHeight.length);
   };
    setCityName(name, code){
        if(code.length > 6) {
            code = code.slice(0, 6);
        }
        this.props.setCity(name, code);
        this.props.navigator.pop();
    }
    rowsMsg(rowData) {
        let rows = JSON.stringify(rowData.children);
        let cities = JSON.parse(rows);
        let first = [];
        for (let i = 0 ;i < cities.length; i++) {
            first.push(
               <TouchableOpacity key={i} onPress={this.setCityName.bind(this,cities[i].Name,cities[i].Code)}>
                   <View style={styles.listItem}>
                       <Text style={{flex:1}}>{cities[i].Name}</Text>
                   </View>
                   <View style={styles.line}></View>
               </TouchableOpacity>
           )
        }
        let result = <View onLayout={
                    (e)=>{
                            RightItemHeight.push(e.nativeEvent.layout.height);
                        }
                    }>
                <View style={styles.listTitle}>
                    <Text style={styles.listTitleText}>{rowData.Name}</Text>
                </View>
                <View>
                    {first}
                </View>
            </View>;
        return (
            <View>{result}</View>
        )
    };

    render(){
        return(
            <ListView dataSource={this.props.dataSource} enableEmptySections = {true} renderRow={(rowData) => this.rowsMsg(rowData)}
            />
        )
    }
}